package com.cqut.ktpspring.service.impl;

import com.cqut.ktpspring.exception.ServiceException;
import com.cqut.ktpspring.mapper.AnswerMapper;
import com.cqut.ktpspring.mapper.CourseMapper;
import com.cqut.ktpspring.mapper.WorkMapper;
import com.cqut.ktpspring.pojo.Course;
import com.cqut.ktpspring.pojo.Work;
import com.cqut.ktpspring.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WorkServiceimpl implements WorkService {
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public List<Work> selectWorks(String cno) {
        return workMapper.selectWorks(cno);
    }

    @Override
    public void insertWork(Work work) {
        Course course = courseMapper.selectById(work.getCourseId());
        if(course == null||course.getDeleted()==1||course.getArchiveStatus()==1) {
            throw new ServiceException("课程被删除或者归档了或者不存在");
        }
        else {
            workMapper.insert(work);
        }

    }

    @Override
    public void updateWork(Work work) {
        workMapper.updateById(work);
    }

    @Override
    @Transactional
    public void deleteWork(String id) {
        workMapper.deleteById(id);
        answerMapper.deleteBywId(id);
    }

}
